package cn.memorynine.consumer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.memorynine.dao.entity.Activity;
import cn.memorynine.dao.entity.Consumer;
import cn.memorynine.dao.entity.Donation;
import cn.memorynine.dao.mapper.ConsumerMapper;
import cn.memorynine.dao.mapper.DonationMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.donation.manage.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* 管理后台-捐赠管理服务
* @author 代码生成器
*/
@Slf4j
@Service
public class DonationManageService {

    @Autowired
    private DonationMapper donationMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    private Snowflake snowflake = IdUtil.getSnowflake(1,1);
    /**
     * 1.查看捐赠列表
     * @return
     */
    public ResponseWrapper donationList(DonationListParam param){
        DonationListResult donationListResult = new DonationListResult();

        //获取总数
        Integer totalNum = donationMapper.selectCount(
                Wrappers.<Donation>lambdaQuery()
                        .like(Donation::getNumber, param.getSearchKey())
        );
        donationListResult.setTotalNum(totalNum);
        //获取分页数据
        Page<Donation> pageInfo = new Page<>(param.getPageNum(), param.getNum(),false);
        List<Donation> donationList = donationMapper.selectPage(pageInfo,
                Wrappers.<Donation>lambdaQuery()
                        .like(Donation::getNumber, param.getSearchKey())
        ).getRecords();
        List<DonationList> donationLists = new ArrayList<>();
        for (Donation donation : donationList) {
            DonationList donationListItem = new DonationList();

            Integer consumerAid = donation.getConsumerAid();
            String name = consumerMapper.selectOne(
                    Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, consumerAid)
            ).getName();
            donationListItem.setConsumerName(name);
            BeanUtil.copyProperties(donation,donationListItem);
            donationLists.add(donationListItem);
        }
        donationListResult.setDonationList(donationLists);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(donationListResult);
    }

    /**
     * 2.新增/修改 捐赠记录
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper saveDonation(SaveDonationParam param){

        if (param.getAid() == -1){
            //新增
            Donation donation = new Donation();
            BeanUtil.copyProperties(param,donation);
            donation.setAid(null);
            donation.setNumber(snowflake.nextIdStr());
            donationMapper.insert(donation);
        } else {
            //修改
            //先扣除之前用户增加的积分，然后在给新/旧用户进行积分增加
            Donation donation = donationMapper.selectOne(
                    Wrappers.<Donation>lambdaQuery()
                            .eq(Donation::getAid, param.getAid())
            );
            Consumer consumer = consumerMapper.selectOne(
                    Wrappers.<Consumer>lambdaQuery()
                            .eq(Consumer::getAid, donation.getConsumerAid())
            );
            //扣除积分
            consumer.setPoints(consumer.getPoints()-donation.getPoints());
            consumerMapper.updateById(consumer);

            //增加积分
            Consumer newConsumer = new Consumer();
            newConsumer = consumerMapper.selectOne(
                    Wrappers.<Consumer>lambdaQuery()
                            .eq(Consumer::getAid, param.getConsumerAid())
            );
            Donation newDonation = new Donation();
            BeanUtil.copyProperties(param,newDonation);
            newConsumer.setPoints(newConsumer.getPoints()+ newDonation.getPoints());

            //修改捐赠信息
            donationMapper.updateById(newDonation);
        }

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 3.修改/新增 捐赠记录-获取捐赠记录信息
     * @return
     */
    public ResponseWrapper donationInfo(DonationInfoParam param){

        DonationInfoResult donationInfoResult = new DonationInfoResult();

        Donation donation = donationMapper.selectOne(
                Wrappers.<Donation>lambdaQuery()
                        .eq(Donation::getAid, param.getAid())
        );

        BeanUtil.copyProperties(donation,donationInfoResult);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess(donationInfoResult);
    }

    /**
     * 4.获取用户信息
     * @return
     */
    public ResponseWrapper consumerList(ConsumerListParam param){

        List<Consumer> consumerList = consumerMapper.selectList(
                Wrappers.<Consumer>lambdaQuery()
                        .like(Consumer::getName,param.getSearchKey())
        );

        List<ConsumerListResult> consumerListResultList = new ArrayList<>();

        for (Consumer consumer : consumerList) {
            ConsumerListResult consumerListResult = new ConsumerListResult();
            BeanUtil.copyProperties(consumer,consumerListResult);
            consumerListResultList.add(consumerListResult);
        }
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(consumerListResultList);
    }
}